package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.entity.User;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private UserServiceImpl userService;


    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        // 1. 获取关注的用户
        Long userId = UserHolder.getUser().getId();
        String key = "follows:" + userId;
        // 2. 判断是关注还是取消关注
        if (isFollow) {
            // 3.关注，添加一条记录
            Follow follow = new Follow();
            follow.setFollowUserId(followUserId);
            follow.setUserId(userId);
            // 增加共同关注数
            boolean isSuccess = this.save(follow);

            if (isSuccess) {
                // 把关注的用户的关注数放入缓存redis
                stringRedisTemplate.opsForSet().add(key, userId.toString());
            }
        } else {
            // 4.取消关注，删除一条记录
            boolean isSuccess = this.remove(new QueryWrapper<Follow>().eq("user_id", userId).eq("follow_user_id", followUserId));
            if (isSuccess) {
                // 4.1 把取消关注的用户的关注数从缓存redis中移除
                 stringRedisTemplate.opsForSet().remove(key, followUserId.toString());
            }

        }
        // 5. 返回结果
        return Result.ok();
    }

    @Override
    public Result isFollow(Long followUserId) {
        // 1. 获取当前用户
        Long userId = UserHolder.getUser().getId();
        // 2. 查询是否关注
        Integer count = query().eq("user_id", userId).eq("follow_user_id", followUserId).count();
        // 3. 返回结果
        return Result.ok(count > 0);
    }

    @Override
    public Result followCommons(Long id) {
        // 1. 获取当前用户
        Long userId = UserHolder.getUser().getId();
        // 2. 查询共同关注.,计算交集
        String key = "follows:" + userId;
        String key2 = "follows:" + id;
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key, key2);
        if (intersect == null || intersect.isEmpty()) {
            // 2.1 无交集，返回空列表
            return Result.ok(Collections.emptyList());
        }
        // 3. 解析交集，返回结果
        List<Long> collect = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        // 4. 查询用户
        List<UserDTO> users = userService.listByIds(collect).stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());
        return Result.ok(users);
    }

}
